package cn.bqjr.eily.samples.controller.common;

import cn.bqjr.eily.mybatis.spring.boot.utils.PagingUtils;
import cn.bqjr.eily.samples.controller.base.BaseController;
import cn.bqjr.eily.samples.entity.po.User;
import cn.bqjr.eily.samples.entity.vo.UserVO;
import cn.bqjr.eily.samples.facade.IUserFacade;
import cn.bqjr.eily.samples.service.base.IUserService;
import cn.bqjr.eily.utils.I18nUtils;
import cn.bqjr.eily.web.modules.apiversion.ApiVersion;
import cn.bqjr.eily.web.utils.AssertUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/{version}/users")
@ApiVersion(1)
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserFacade userFacade;

    /**
     * 新增用户
     * @param userVO
     * @return
     */
    @PostMapping
    public Object addUser(@RequestBody UserVO userVO) throws Exception {
        AssertUtils.equals(userService.selectCount(
                new EntityWrapper<User>().eq(User.LOGINID, userVO.getLoginId())),
                0, I18nUtils.getMsg("error.request.duplicateRecord"));
        return userFacade.insertVO(userVO);
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping(value = "/{userId}")
    public Object deleteUser(@PathVariable("userId") Long userId) {
        return userFacade.deleteUser(userId);
    }

    /**
     * 批量删除用户
     * @param userIds
     * @return
     */
    @DeleteMapping
    public Object deleteUserByBatchId(@RequestParam("ids") List<Long> userIds) {
        return userFacade.deleteUserByBatchId(userIds);
    }

    /**
     * 更新用户
     * @param userId
     * @param userVO
     * @return
     */
    @PutMapping(value = "/{userId}")
    public Object updateUser(@PathVariable("userId") Long userId,
                             @RequestBody UserVO userVO) throws Exception {
        AssertUtils.equals(userId, userVO.getUserId());

        User user = userService.selectById(userId);
        AssertUtils.notNull(user, I18nUtils.getMsg("error.request.inValidRecord"));

        userVO.setLoginId(user.getLoginId());
        return userFacade.updateVO(userVO);
    }

    /**
     * 查询指定用户
     * @param userId
     * @return
     */
    @GetMapping(value = "/{userId}")
    public Object selectUser(@PathVariable("userId") Long userId) {
        return userService.selectById(userId);
    }


    /**
     * 查询全部用户
     * @return
     */
    @GetMapping
    public Object selectAllUsers() {
        PagingUtils.pagingIfExist(request);
        return userService.selectList(new EntityWrapper<>());
    }

    /**
     * 查询指定用户的菜单列表
     * @param userId
     * @return
     */
    @GetMapping(value = "/{userId}/menus")
    public Object selectUserMenuList(@PathVariable("userId") Long userId) {
        AssertUtils.notNull(selectUser(userId), I18nUtils.getMsg("error.request.illegalArgument"));
        return userFacade.selectUserMenus(userId);
    }

    /**
     * 查询指定用户的权限列表
     * @param userId
     * @return
     */
    @GetMapping(value = "/{userId}/perms")
    public Object selectUserPermList(@PathVariable("userId") Long userId) {
        AssertUtils.notNull(selectUser(userId), I18nUtils.getMsg("error.request.illegalArgument"));
        return userFacade.selectUserPerms(userId);
    }

    /**
     * 查询指定用户的角色列表
     * @param userId
     * @return
     */
    @GetMapping(value = "/{userId}/roles")
    public Object selectUserRoleList(@PathVariable("userId") Long userId) {
        AssertUtils.notNull(selectUser(userId), I18nUtils.getMsg("error.request.illegalArgument"));
        return userFacade.selectUserRoleIdList(userId);
    }
}
